package com.macro.mall.portal.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.domain.CreateProductParam;
import com.macro.mall.domain.PmsProductQueryParam;
import com.macro.mall.model.PmsProduct;
import com.macro.mall.portal.service.PmsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
@RestController
@Api(tags = "PmsProductController", description = "门户：商品管理")
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private PmsProductService pmsProductService;

    @ApiOperation("创建商品")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult create(@RequestParam String reqJson) {
        CreateProductParam createProductParam = JSONArray.parseObject(reqJson, CreateProductParam.class);
        int count = pmsProductService.create(createProductParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改商品")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult update(@RequestParam String reqJson) {
        CreateProductParam createProductParam = JSONArray.parseObject(reqJson, CreateProductParam.class);
        int count = pmsProductService.update(createProductParam.getProductParam().getId(), createProductParam.getProductParam());
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("查询商品")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<PageInfo<PmsProduct>> getList(PmsProductQueryParam productQueryParam,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        log.info("分页信息为：页码-{},页长-{}", pageNum, pageSize);
        List<PmsProduct> productList = pmsProductService.list(productQueryParam, pageSize, pageNum);
        PageInfo<PmsProduct> pageInfo = new PageInfo<>(productList);
        return CommonResult.success(pageInfo);
    }

    @ApiOperation("批量上下架")
    @RequestMapping(value = "/update/publishStatus", method = RequestMethod.POST)
    public CommonResult updatePublishStatus(@RequestParam("ids") List<Long> ids,
                                            @RequestParam("publishStatus") Integer publishStatus) {
        int count = pmsProductService.updatePublishStatus(ids, publishStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除商品")
    @RequestMapping(value = "/update/deleteStatus", method = RequestMethod.POST)
    public CommonResult updateDeleteStatus(@RequestParam("ids") List<Long> ids,
                                           @RequestParam("publishStatus") Integer publishStatus) {
        int count = pmsProductService.updateDeleteStatus(ids, publishStatus);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("批量设置好物商品(修改审批状态为待审核)")
    @RequestMapping(value = "/update/recommendStatus", method = RequestMethod.POST)
    public CommonResult updateRecommendStatus(@RequestParam("ids") List<Long> ids,
                                              @RequestParam("newStatus") Integer newStatus) {
        int count = pmsProductService.updateVerifyStatus(ids, newStatus, null);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("查询待审核的好物商品")
    @RequestMapping(value = "/update/getNewList", method = RequestMethod.POST)
    public CommonResult getNewList() {
        List<PmsProduct> newList = pmsProductService.getNewList();
        return CommonResult.success(newList);
    }
}
